layui.use(['common', 'layer', 'table', 'form', ], function() {
    var $ = layui.$,
        layer = layui.layer,
        form = layui.form,
        common = layui.common,
        table = layui.table;
    var id = GetQueryString("id");
    
    $.post("/systemsetup/permissionsmanage/role/selectAdminRoleById", {
        id: id
    }, function(result) {
        $("#name").val(result.data["name"]);
        $("#title").val(result.data["title"]);
        $("#description").val(result.data["description"]);
        $("#orders").val(result.data["orders"]);
        $("#roleId").val(result.data["roleId"]);
    });
   
    
    //监听提交
    form.on('submit(roleEdit)', function(data) {
       $.post("/systemsetup/permissionsmanage/role/updataAdminRole", data.field, function(result) {
            var zTree = $("#zTreeResults").val()
            if (zTree == "") {
                layer.msg("请选择权限");
            }
            if (result.status == 200) {
                parent.layer.closeAll();
            } else {
                layer.msg(result.msg);
            }
        });
        return false;
    });
    
    function zTreeResult() {
        var $j = jQuery.noConflict();
        var treeObj = $j.fn.zTree.getZTreeObj("treeDemo"),
            nodes = treeObj.getCheckedNodes(true),
            v = "";
        permissionsIds = "";
        for (var i = 0; i < nodes.length; i++) {
            v += nodes[i].name + ",";
            permissionsIds += nodes[i].id + ","; //获取选中节点的值
        }
        return permissionsIds;
    }
    
    common.larryCmsLoadJq('/LarryMS/business/common/ztree/js/jquery.ztree.all.js', function() {
        var zTreeObj,
            setting = {
                data: {
                    simpleData: {
                        enable: true,
                    }
                },
                check: {
                    enable: true
                },
                callback: {
                    onClick: function(e, treeId, treeNode, clickFlag) {
                        zTreeObj.checkNode(treeNode, !treeNode.checked, true);
                        $("#zTreeResults").val(zTreeResult());
                    }
                }
            }
        var $j = jQuery.noConflict();
        var zNodes = "";

        function Inint() { //初始化加载节点
            $.ajax({
                global: true,
                async: false,
                cache: false,
                dataType: "json",
                url: '/systemsetup/permissionsmanage/permissions/selectPermissions',
                type: "post",
                //data: { "roleId": GetQueryString("roleId") },
                success: function(result) {
                    zNodes = result.data;
                }
            });
        };
        $(document).ready(function() {
            Inint(); //加载数据
            zTreeObj = $j.fn.zTree.init($("#treeDemo"), setting, zNodes);
            var treeObj = $j.fn.zTree.getZTreeObj("treeDemo");
            var nodes = treeObj.getNodes();
            for (var i = 0; i < nodes.length; i++) { //设置节点展开
                treeObj.expandNode(nodes[i], true, true, true);
            }
        });

        function getAllChildrenNodes(treeNode, result) {
            if (treeNode.isParent) {
                var childrenNodes = treeNode.children;
                if (childrenNodes) {
                    for (var i = 0; i < childrenNodes.length; i++) {
                        result += ',' + childrenNodes[i].id;
                        result = getAllChildrenNodes(childrenNodes[i], result);
                    }
                }
            }
            return result;
        }
    });
});